package com.platform.system.service.impl;

import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.Constants;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.ChatBackgroundMusicDto;
import com.platform.system.domain.dto.device.SpeakerInfoDto;
import com.platform.system.domain.dto.device.SpeakerTypeDto;
import com.platform.system.domain.entity.device.ChatBackgroundMusic;
import com.platform.system.domain.entity.device.SpeakerInfo;
import com.platform.system.domain.entity.device.SpeakerType;
import com.platform.system.domain.vo.device.ChatBackgroundMusicVo;
import com.platform.system.mapper.device.ChatBackgroundMusicMapper;
import com.platform.system.service.ISysChatBackgroundMusicService;
import com.platform.system.service.ISysSpeakerTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class SysChatBackgroundMusicServiceImpl extends ServiceImpl<ChatBackgroundMusicMapper, ChatBackgroundMusic> implements ISysChatBackgroundMusicService {

    @Resource
    ChatBackgroundMusicMapper chatBackgroundMusicMapper;

    @Resource
    ISysChatBackgroundMusicService sysChatBackgroundMusicService;

    @Resource
    ISysSpeakerTypeService sysSpeakerTypeService;


    @Override
    public TableDataInfo selectChatBackgroundMusicList(ChatBackgroundMusicVo chatBackgroundMusicVo) {
        LambdaQueryWrapper<ChatBackgroundMusic> musicLambdaQueryWrapper = new QueryWrapper<ChatBackgroundMusic>().lambda();
        musicLambdaQueryWrapper.like(StringUtils.isNotEmpty(chatBackgroundMusicVo.getName()), ChatBackgroundMusic::getName, chatBackgroundMusicVo.getName());
        musicLambdaQueryWrapper.orderByDesc(ChatBackgroundMusic::getCreateTime);
        Page<ChatBackgroundMusic> page = new Page<>();
        page.setCurrent(chatBackgroundMusicVo.getPageNum());
        page.setSize(chatBackgroundMusicVo.getPageSize());
        IPage<ChatBackgroundMusic> backgroundMusicPage = chatBackgroundMusicMapper.selectPage(page, musicLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<ChatBackgroundMusic> list =  backgroundMusicPage.getRecords();
        List<ChatBackgroundMusicDto> chatBackgroundMusicDtoList = new ArrayList<>();
        if(list != null && list.size() > 0){
            List<String> styleUidList  = list.stream().map(ChatBackgroundMusic::getStyleUid).collect(Collectors.toList());
            Map<String, List<SpeakerType>> map = sysSpeakerTypeService.selectSpeakerTypeByUidList(styleUidList).stream().collect(
                    Collectors.groupingBy(
                            SpeakerType::getUid));
            String array = JSONArray.toJSONString(list);
            chatBackgroundMusicDtoList = JSONArray.parseArray(array, ChatBackgroundMusicDto.class);
            chatBackgroundMusicDtoList.forEach(chatBackgroundMusicDto -> {
                if(map.containsKey(chatBackgroundMusicDto.getStyleUid())){
                    SpeakerType speakerType = map.get(chatBackgroundMusicDto.getStyleUid()).get(0);
                    chatBackgroundMusicDto.setStyleName(speakerType.getStyleName());
                }
            });
        }
        rspData.setRows(chatBackgroundMusicDtoList);
        rspData.setTotal(backgroundMusicPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public AjaxResult insertChatBackgroundMusic(ChatBackgroundMusicVo chatBackgroundMusicVo) {
        ChatBackgroundMusic chatBackgroundMusic = new ChatBackgroundMusic();
        chatBackgroundMusic.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        chatBackgroundMusic.setFileUrl(chatBackgroundMusicVo.getFileUrl());
        chatBackgroundMusic.setName(chatBackgroundMusicVo.getName());
        chatBackgroundMusic.setParam(chatBackgroundMusicVo.getParam());
        chatBackgroundMusic.setStyleUid(chatBackgroundMusicVo.getStyleUid());
        chatBackgroundMusic.setIsDel(Constants.NUM_ZERO);
        chatBackgroundMusic.setSort(Constants.NUM_ZERO);
        chatBackgroundMusic.setCreateTime(new Date());
        chatBackgroundMusic.insert();
        return AjaxResult.success();
    }

    @Override
    public ChatBackgroundMusicDto selectChatBackgroundMusic(String uid) {
        LambdaQueryWrapper<ChatBackgroundMusic> musicLambdaQueryWrapper = new QueryWrapper<ChatBackgroundMusic>().lambda();
        musicLambdaQueryWrapper.eq(ChatBackgroundMusic::getUid, uid);
        ChatBackgroundMusic chatBackgroundMusic = chatBackgroundMusicMapper.selectOne(musicLambdaQueryWrapper);
        if(chatBackgroundMusic != null) {
            ChatBackgroundMusicDto chatBackgroundMusicDto = new ChatBackgroundMusicDto();
            BeanUtils.copyProperties(chatBackgroundMusic, chatBackgroundMusicDto);
            return chatBackgroundMusicDto;
        }
        return null;
    }

    @Override
    public AjaxResult updateChatBackgroundMusic(ChatBackgroundMusicVo chatBackgroundMusicVo) {
        ChatBackgroundMusicDto chatBackgroundMusicDto = sysChatBackgroundMusicService.selectChatBackgroundMusic(chatBackgroundMusicVo.getUid());
        if(chatBackgroundMusicDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        ChatBackgroundMusic chatBackgroundMusic = new ChatBackgroundMusic();
        LambdaQueryWrapper<ChatBackgroundMusic> musicLambdaQueryWrapper = new QueryWrapper<ChatBackgroundMusic>().lambda();
        musicLambdaQueryWrapper.eq(ChatBackgroundMusic::getUid, chatBackgroundMusicVo.getUid());
        BeanUtils.copyProperties(chatBackgroundMusicVo, chatBackgroundMusic);
        chatBackgroundMusic.setUpdateTime(new Date());
        chatBackgroundMusic.update(musicLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteChatBackgroundMusic(ChatBackgroundMusicVo chatBackgroundMusicVo) {
        LambdaQueryWrapper<ChatBackgroundMusic> musicLambdaQueryWrapper = new QueryWrapper<ChatBackgroundMusic>().lambda();
        musicLambdaQueryWrapper.eq(ChatBackgroundMusic::getUid, chatBackgroundMusicVo.getUid());
        chatBackgroundMusicMapper.delete(musicLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public Long selectChatBackgroundMusicCountByStyleUid(String styleUid) {
        LambdaQueryWrapper<ChatBackgroundMusic> musicLambdaQueryWrapper = new QueryWrapper<ChatBackgroundMusic>().lambda();
        musicLambdaQueryWrapper.eq(ChatBackgroundMusic::getStyleUid, styleUid);
        Long selectCount = chatBackgroundMusicMapper.selectCount(musicLambdaQueryWrapper);
        return selectCount;
    }
}
